package dk.bris.webdev;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class MyServlet
 */
@WebServlet("/MyServlet")
public class MyServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * Default constructor.
	 */
	public MyServlet() {
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setDateHeader("Expires", 0);
		response.setHeader("Cache-Control",
				"no-store, no-cache, must-revalidate");
		response.setHeader("Pragma", "no-cache");
		PrintWriter out = response.getWriter();
		try {

			List<GuestEntry> list = getList();
			doHTMLResponse(request, response, list);
		} catch (Exception e) {
			if (out != null) {
				out.println("<html><head><title>GUESTBOOK</title></head><body>"
						+ e.getMessage() + "</body></html>");
			}
		}

	}

	private void doHTMLResponse(HttpServletRequest request,
			HttpServletResponse response, List<GuestEntry> list)
			throws IOException {

		PrintWriter out = response.getWriter();
		out.println("<html><head><title>GUESTBOOK</title></head><body>");

		if (list.size() > 0) {
			out.print("<table border=\"1\">");
			for (GuestEntry entry : list) {
				out.print("<tr>");
				out.print("<td>" + entry.getTime() + "</td>");
				out.print("<td>" + entry.getUser() + "</td>");
				out.print("<td>" + entry.getMessage() + "</td>");
				out.print("</tr>");
			}
			out.print("</table>");
		}

		out.print("<br><br>Enter new message?<br>");
		out.print("<form method=post action=MyServlet>");
		out.print("Username: <input name=name type=text><br>"
				+ "Message: <input name=text type=text><br>"
				+ "<input type=submit name=submit value=Send>" + "</form>");

		// out.println("The value of the <tt>name</tt> field is: <tt>"+
		// htmlEscape(name) + "</tt>");
		out.println("</body></html>");

	}

	private List<GuestEntry> getList() {
		Object o = getServletContext().getAttribute("guestbook");
		List<GuestEntry> list;
		if (o == null) {
			list = new ArrayList<GuestEntry>();
		} else {
			list = (List<GuestEntry>) o;
		}
		return list;
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {

		String user = request.getParameter("name");
		String message = request.getParameter("text");
		Date d = new Date();

		List<GuestEntry> list = getList();
		list.add(new GuestEntry(message, user, d));
		getServletContext().setAttribute("guestbook", list);
		doHTMLResponse(request, response, list);
	}

}
